home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SerialDriverArbitration.c
-
- Contains: This is an example of how to correctly arbitrate the serial ports. It's taken
- from tech note DV 11 - Opening the Serial Ports.
-
- Written by: Brian Bechtel. Updated by Lenae Rowland.
-
- Copyright: Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/7/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
- 12/5/96 Lenae Rowland Updated
- 10/7/93 Brian Bechtel Created
-
-
- */
- // Comments beginning with -LR- are changes made to
- // accomodate new Universal Headers
-
- // -LR- Changed GestaltEqu.h to Gestalt.h
- #include <Gestalt.h>
- // -LR- Changed SysEqu.h to LowMem.h
- #include <LowMem.h>
- #include <Devices.h>
-
- #include <stdio.h>
- #include <StringCompare.h>
-
- #define gestaltArbitorAttr 'arb '
- #define gestaltSerialArbitrationExists 0
- #define dRAMBased 0x0040
- #define dOpened 0x0020
-
- // This is a list of drivers used for test purposes in this code
- Str255 gDriverNames[] =
- {
- "\p.NoDrvr", // non-existant driver
- "\p.AOut", // serial port A output
- "\p.BIn", // serial port B input
- "\p.BOut", // serial port B output
- "\p.AppleCD", // CD-ROM output
- "\p.MPP", // AppleTalk
- "\p.ASYC00", // a hard disk driver
- "\p.AIn" // serial port A input
- };
-
-
- Boolean SerialArbitrationExists(void);
- Boolean DriverIsOpen(StringPtr driverName);
- Boolean CRMInstalled(void);
-
- // Test Gestalt to see if serial arbitration exists
- // on this machine
- Boolean SerialArbitrationExists(void)
- {
- long response;
- OSErr err;
-
- err = Gestalt(gestaltArbitorAttr, &response);
- if (err)
- return false;
- return ((response >> gestaltSerialArbitrationExists) & 1);
- }
-
-
- Boolean DriverIsOpen(StringPtr driverName)
- {
- Boolean canOpen = false;
- Boolean match = false;
- short index = 0;
- short count;
- DCtlHandle dceHandle;
- StringPtr namePtr;
- DCtlHandle *theUnitTable;
-
- // -LR- changed this to a low memory accessor function
- count = LMGetUnitTableEntryCount();
-
- // -LR- changed this to a low memory accessor function
- theUnitTable = (DCtlHandle*)LMGetUTableBase();
-
- while ( !match && (index < count)) {
- // get handle to a device control entry
- dceHandle = theUnitTable[index];
-
- if (dceHandle) {
- if (!( (**dceHandle).dCtlFlags & dRAMBased) )
- // RAM based drivers have a handle to their driver
- namePtr = (StringPtr)(**dceHandle).dCtlDriver+18;
- else
- // ROM based drivers have a pointer to their driver
- namePtr = (StringPtr) (*(DCtlPtr)dceHandle).dCtlDriver+18;
-
- // not case sensitive, diacritical marks count
- if (RelString(driverName, namePtr, false, true) == 0) {
- match = true;
- canOpen = ((**dceHandle).dCtlFlags & dOpened) ? true : false;
- }
- }
- index++;
- }
- return canOpen;
- }
-
- Boolean CRMInstalled(void)
- {
- long response;
- OSErr err;
-
- err = Gestalt(gestaltCRMAttr, &response);
- if (err)
- return false;
- return ((response >> gestaltCRMPresent) & 1);
- }
-
- #define TEST_CALL
- void main()
- {
- OSErr err = noErr;
- SInt16 i;
- SInt16 numDrivers;
-
- printf("Test of serial port stuff\n");
- if (CRMInstalled())
- printf("Communication Resource Manager is installed.\n");
- if (SerialArbitrationExists())
- printf("Serial Arbitration exists\n");
-
- #ifdef TEST_CALL
- numDrivers = sizeof(gDriverNames) / sizeof(Str255);
- for (i = 0; i < numDrivers; i++)
- printf("DriverIsOpen(%#s)\treturned %s\n",
- gDriverNames[i], DriverIsOpen(gDriverNames[i])?"True ":"False");
- #endif
-
- #ifdef TEST_OPEN
- SInt16 refNum;
-
- if (!DriverIsOpen("\p.AIn"))
- err = OpenDriver("\p.AIn", &refNum);
- if (err)
- printf("OpenDriver returned %d\n", err);
- err = CloseDriver(refNum);
- if (err)
- printf("CloseDriver returned %d\n", err);
- #endif
- }